package exercises;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28779
 * Date: 2023-04-16
 * Time: 13:58
 */
public class Test {
    public static void main(String[] args) {
        class Solution {
            public boolean isValid(String s) {

                Stack<Character> stack = new Stack<>();

                for(int i = 0; i < s.length(); i ++) {
                    char ch = s.charAt(i);
                    if(ch == '{' || ch == '[' || ch == '(') {
                        stack.push(ch);
                    } else {
                        if(stack.empty()) {
                            return false;
                        } else {
                            char temp = stack.peek();
                            if(ch == '}' && temp == '{' || ch == ']' && temp == '[' || ch == ')' && temp == '(') {
                                stack.pop();
                            } else {
                                return false;
                            }
                        }
                    }
                }
                if(!stack.empty()) {
                    return false;
                }
                return true;

            }
        }
    }
    public static void main1(String[] args) {
        class Solution {
            public int evalRPN(String[] tokens) {
                Stack<Integer> stack = new Stack<>();

                for(String s : tokens) {
                    if(!isOpera(s)) {
                        stack.push(Integer.parseInt(s));
                    }else {
                        int num2 = stack.pop();
                        int num1 = stack.pop();
                        switch(s) {
                            case "+" :
                                stack.push(num1+num2);
                                break;
                            case "-" :
                                stack.push(num1-num2);
                                break;
                            case "*" :
                                stack.push(num1*num2);
                                break;
                            case "/" :
                                stack.push(num1/num2);
                                break;
                        }
                    }
                }
                return stack.pop();
            }

            public boolean isOpera(String s){
                if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
                    return true;
                }
                return false;
            }
        }

    }
}
